var Admin = Class.create();

Object.extend(Admin, {
  ATTACHABLE_PHOTO_SETS: [],
  ATTACHABLE_GOALS: [],

  selectAttachmentType: function(type) {
    switch(type) {
      case "photos":
        target = Admin.ATTACHABLE_PHOTO_SETS;
        target_type = 'photo_set';
        break;
      case "goals":
        target = Admin.ATTACHABLE_GOALS;
        target_type = 'goal';
        break;
      default:
        alert("Unrecognized attachment type.");
        return false;
    }
  
    if (Admin.populateAttachmentType(type)) {
      el = new Element('select');
      el.setAttribute('class', 'default');
      el.setAttribute('name', 'attachments[]');
      el.setAttribute('style', 'margin-right: 10px;');
      
      // Default option
      option = new Element('option');
      option.setAttribute('class', 'default');
      option.setAttribute('selected', 'selected');
      option.setAttribute('value', '');
      option.update(' - ');
      
      el.insert({top: option});
      
      target.each(function(attachable) {
        option = new Element('option');
        option.setAttribute('class', 'default');
        option.setAttribute('value', target_type + ',' + attachable[0]);
        option.update(attachable[1]);
        
        el.insert(option);
      });
      
      $('attachable_container').insert(el);
    }
    
    return false;
  },
  
  populateAttachmentType: function(type) {
    if (target.size() == 0) {
      new Ajax.Request('/admin/get_attachables/' + target_type, {
        method: 'get',
        asynchronous: false,
        onSuccess: function(res) {
          target = eval(res.responseText);
          if (type == "photos") 
            Admin.ATTACHABLE_PHOTO_SETS = target;
          else if (type == "goals") 
            Admin.ATTACHABLE_GOALS = target;
        },
        onFailure: function(res) {
          alert("Error retrieving attachables.");
          return false;
        }
      });
    }
    
    return true;
  },
  
  SONG_TEXT: '<center><object width="400" height="15"><param name="movie" value="/xspf_player_slim.swf"></param><param name="player_title" value="%TITLE%"></param><param name="song_url" value="%LINK%"></param><param name="song_title" value="%TITLE%"></param><embed src="/xspf_player_slim.swf" type="application/x-shockwave-flash" flashvars="player_title=%TITLE%&song_url=%LINK%&song_title=%TITLE%" width="400" height="15"></embed></object></center>',
  addSong: function() {
    title = prompt("Song Title", null);
    link  = prompt("Song URL", null);
    
    if ($('blog_body') && (title != null) && (link != null)) {
      formatted = Admin.SONG_TEXT.gsub('%TITLE%', title).gsub('%LINK%', link);
      
      $('blog_body').value = formatted + $('blog_body').value;
      $('blog_body').activate();
    }
  },
  
  reorderPhotoSet: function() {
    var photo_positions = $$('#photos-list input.position');
    var i = 0;

    // update queue positions based on HTML ordering
    photo_positions.each(function(input) {
      if (/^position_/.test(input.name)) {
        input.value = i;
        i++;
      }
    });
  }
});

